package superhero.gui;

import JFileChooserPreview.ImageFileView;
import JFileChooserPreview.ImageFilter;
import JFileChooserPreview.ImagePreview;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.IOException;
import javax.swing.JComboBox;
import superhero.data.SuperheroDBQuery;

/**
 *
 * @author jimsonm0466
 */
public class CustomDisplayPanel
        extends JFrame
{

    private JPanel panel = new JPanel(new BorderLayout());
    private JPanel customPanel = new JPanel();
    private JPanel[] holdLabels = new JPanel[5];
    private JPanel[] temp = new JPanel[5];
    private JLabel[] labels = new JLabel[5];
    private JTextField[] inputs = new JTextField[4];
    private JTextField imageFile = new JTextField(20);
    private ImageIcon picture;
    private JPanel previewPanel, hold;
    private File selectedFile;
    private String fileName;
    private JComboBox factions;

    /**
     * CustomDisplayPanel brings up a window for the user
     * to build their own superhero and save it to the DATABASE.
     */
    public CustomDisplayPanel()
    {
        setTitle("Create Your Own Hero");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.add(panel);

        buildCustomPanel();
        panel.add(customPanel,
                BorderLayout.CENTER);
        JPanel tempPanel = new JPanel();
        JButton save = new JButton("Save");
        save.addActionListener(new SaveButtonListener());
        JButton preview = new JButton("Preview Hero");
        preview.addActionListener(new PreviewButtonListener());
        tempPanel.add(preview);
        tempPanel.add(save);
        panel.add(tempPanel,
                BorderLayout.SOUTH);
        this.pack();
        this.setVisible(true);
    }

    private void buildCustomPanel()
    {
        hold = new JPanel(new GridLayout(5,
                2));
        hold.setPreferredSize(new Dimension(450,
                450));
        String[] labelNames =
        {
            "What is your Faction name: ",
            "Your Hero Name:",
            "What is your real name:",
            "What are your super powers:",
            "Choose image for hero:"
        };

        for (int i = 0;
                i < 5;
                i++)
        {
            holdLabels[i] = new JPanel();
            temp[i] = new JPanel();
            labels[i] = new JLabel(labelNames[i]);
            holdLabels[i].add(labels[i]);
        }
        
        String names = "SELECT FactionName "
                + "FROM Factions ";

        SuperheroDBQuery faction =
                new SuperheroDBQuery(names);
        
        factions = new JComboBox(faction.getRecords());
        factions.setSelectedItem("");
        factions.setEditable(true);
        temp[0].add(factions);

        for (int i = 1;
                i < 4;
                i++)
        {
            inputs[i] = new JTextField(20);
            temp[i].add(inputs[i]);
        }        

        temp[4].add(imageFile);
        JButton browse = new JButton("Browse");
        browse.addActionListener(new BrowseButtonListener());
        temp[4].add(browse);

        for (int i = 0;
                i < 5;
                i++)
        {
            hold.add(holdLabels[i]);
            hold.add(temp[i]);
        }
        pack();
        customPanel.add(hold,
                BorderLayout.NORTH);

    }
    
    /**
     * This method builds the Preview Screen for the user to view
     * their custom hero before saving it to the DATABASE.
     */
    private void buildPreviewScreen()
    {
        JFrame window = new JFrame();
        JPanel basic = new JPanel(new BorderLayout());

        previewPanel = new JPanel(new BorderLayout());
        hold = new JPanel(new GridLayout(6,
                1));
        JPanel[] temps = new JPanel[3];
        JPanel[] textFields = new JPanel[3];
        String[] lblNames =
        {
            "Character Name:",
            "Real Name:",
            "Powers:"
        };
        JLabel[] lbls = new JLabel[3];
        Font font = new Font("Verdana",
                Font.BOLD,
                12);

        for (int i = 0;
                i < 3;
                i++)
        {
            temps[i] = new JPanel();
            textFields[i] = new JPanel();
            lbls[i] = new JLabel(lblNames[i]);
            temps[i].add(lbls[i]);
            hold.add(temps[i]);
            int x = i + 1;
            textFields[i].add(inputs[x]);
            inputs[x].setHorizontalAlignment(JTextField.CENTER);
            inputs[x].setEditable(false);
            inputs[x].setFont(font);
            hold.add(textFields[i]);
        }

        JLabel picHolder = new JLabel(picture);
        previewPanel.add(hold,
                BorderLayout.CENTER);
        previewPanel.add(picHolder,
                BorderLayout.WEST);
        pack();
        previewPanel.setVisible(true);

        basic.add(previewPanel);
        window.add(basic);
        window.pack();
        window.setVisible(true);
    }
    
    private class BrowseButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            {
                JFileChooser fileChooser =
                        new JFileChooser("Images\\HeroImage");
                
                 fileChooser.addChoosableFileFilter(new ImageFilter());
            fileChooser.setAcceptAllFileFilterUsed(false);

	    //Add custom icons for file types.
            fileChooser.setFileView(new ImageFileView());

	    //Add the preview pane.
            fileChooser.setAccessory(new ImagePreview(fileChooser));
            
                int status = fileChooser.showOpenDialog(null);

                if (status == JFileChooser.APPROVE_OPTION)
                {
                    selectedFile = fileChooser.getSelectedFile();
                    fileName = selectedFile.getPath();
                    imageFile.setText(fileName);


                    picture = new ImageIcon(fileName);
                    pack();
                }
                else if (status == JFileChooser.CANCEL_OPTION)
                {
                    JOptionPane.showMessageDialog(null,
                            "You canceled");
                }
                else if (status == JFileChooser.ERROR_OPTION)
                {
                    JOptionPane.showMessageDialog(null,
                            "There was an error, or"
                            + " you decided to use the STANDARD close button.");
                }

            }
        }
    }

    private class PreviewButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            buildPreviewScreen();
        }
    }
    
    private class SaveButtonListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            String pathName = inputs[1].getText();
            try
            {
                Path source = selectedFile.toPath();
                Path newdir = FileSystems.getDefault().
                        getPath("Images\\");
                Files.copy(source,
                        newdir.resolve(pathName + ".jpg"));
            }
            catch (IOException x)
            {
                System.err.print("Wrong PathName " + x);
            }
            
            SuperheroDBQuery dbConn = new SuperheroDBQuery();            
            dbConn.addSuperHero(factions.getSelectedItem().toString().trim(),
                    inputs[1].getText().trim(), 
                    inputs[2].getText().trim(), inputs[3].getText().trim());
            dbConn.closeConnection();
            
            JOptionPane.showMessageDialog(customPanel,                                   
                                    "Your Hero is saved!!");
            dispose();
        }
    }
}
